package smartcarpool.smartspaceservice;

import android.app.PendingIntent;
import android.util.Log;
import sofia_kp.KPICore;

import java.util.Vector;


public class SmartSpaceRemove implements Runnable {

	private KPICore smartSpace;
	private String HOST;
	private int PORT;
	private String SPACE_NAME;
	private Vector<Vector<String>> tripleVector;
    private PendingIntent pendingIntent;

	public SmartSpaceRemove(String HOST, int PORT, String SPACE_NAME, Vector<Vector<String>> tripleVectorToRemove, PendingIntent pendingIntent) {
		this.HOST = HOST;
		this.PORT = PORT;
		this.SPACE_NAME = SPACE_NAME;
		this.tripleVector = tripleVectorToRemove;
        this.pendingIntent = pendingIntent;
	}

	@Override
	public void run() {
        try {
            smartSpace = new KPICore(this.HOST, this.PORT, this.SPACE_NAME);
            String joinResult = smartSpace.join();
            if (smartSpace.xmlTools.isJoinConfirmed(joinResult)) {
                String removeResult = smartSpace.remove(tripleVector);
                    if (smartSpace.xmlTools.isInsertConfirmed(removeResult)) {
                        pendingIntent.send(SmartSpaceService.RESULT_SUCCESS_REMOVE);
                        Log.d("SmartSpaceRemove "," Information has been removed");
                    } else {
                        pendingIntent.send(SmartSpaceService.RESULT_FAILURE_REMOVE);
                        Log.d("SmartSpaceRemove "," Error, while removing the information.");
                    }
            } else {
                pendingIntent.send(SmartSpaceService.RESULT_FAILURE_JOIN);
            }
            smartSpace.leave();
        } catch (PendingIntent.CanceledException e){
            Log.e("SmartSpaceRemove. Pending Intent Error, Canceled", e.toString());
            e.printStackTrace();
        } catch (Exception e){
            Log.e("SmartSpaceRemove Error", e.toString() + "\n String to remove" +tripleVector.toString());
            e.printStackTrace();
        }
	}
}
